//#include <QApplication>
//#include "ElaApplication.h"
//#include "ElaTheme.h"
//#include "ElaWidget.h"
//#include "ElaCheckBox.h"
//#include <QVBoxLayout>
//#include <QHBoxLayout>
//#include <QGroupBox>
//#include <QLabel>
//#include <QPushButton>
//#include <QDebug>

#include <QApplication>
#include "ElaApplication.h"
#include "ElaTheme.h"
#include "ElaWidget.h"
#include "ElaText.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGroupBox>
#include <QLabel>
#include <QPushButton>
#include <QDebug>

class ElaTextTest : public ElaWidget
{
    Q_OBJECT

public:
    ElaTextTest(QWidget *parent = nullptr) : ElaWidget(parent)
    {
        setWindowTitle("ElaText 功能演示");
        resize(800, 700);
        setWindowButtonFlags(ElaAppBarType::StayTopButtonHint | 
                           ElaAppBarType::MinimizeButtonHint | 
                           ElaAppBarType::MaximizeButtonHint | 
                           ElaAppBarType::CloseButtonHint | 
                           ElaAppBarType::ThemeChangeButtonHint);

        setupUI();
        setupConnections();
    }

private:
    void setupUI()
    {
        QVBoxLayout *mainLayout = new QVBoxLayout(this);

        // 1. 文本样式展示
        mainLayout->addWidget(createTextStyleGroup());

        // 2. 图标文本展示
        mainLayout->addWidget(createIconTextGroup());

        // 3. 换行模式展示
        mainLayout->addWidget(createWrapModeGroup());

        // 4. 动态操作演示
        mainLayout->addWidget(createDynamicOperationGroup());

        // 5. 实际应用场景
        mainLayout->addWidget(createApplicationGroup());

        mainLayout->addStretch();
    }

    // 创建文本样式展示组
    QGroupBox *createTextStyleGroup()
    {
        QGroupBox *group = new QGroupBox("文本样式展示", this);
        QVBoxLayout *layout = new QVBoxLayout(group);

        QLabel *infoLabel = new QLabel("展示 ElaText 支持的多种文本样式", this);
        layout->addWidget(infoLabel);

        // 不同文本样式
        ElaText *captionText = new ElaText("Caption 样式 - 12px 常规字体", this);
        captionText->setTextStyle(ElaTextType::Caption);

        ElaText *bodyText = new ElaText("Body 样式 - 13px 常规字体", this);
        bodyText->setTextStyle(ElaTextType::Body);

        ElaText *bodyStrongText = new ElaText("BodyStrong 样式 - 13px 粗体", this);
        bodyStrongText->setTextStyle(ElaTextType::BodyStrong);

        ElaText *subtitleText = new ElaText("Subtitle 样式 - 20px 粗体", this);
        subtitleText->setTextStyle(ElaTextType::Subtitle);

        ElaText *titleText = new ElaText("Title 样式 - 28px 粗体", this);
        titleText->setTextStyle(ElaTextType::Title);

        ElaText *titleLargeText = new ElaText("TitleLarge 样式 - 40px 粗体", this);
        titleLargeText->setTextStyle(ElaTextType::TitleLarge);

        ElaText *displayText = new ElaText("Display 样式 - 48px 粗体", this);
        displayText->setTextStyle(ElaTextType::Display);

        layout->addWidget(captionText);
        layout->addWidget(bodyText);
        layout->addWidget(bodyStrongText);
        layout->addWidget(subtitleText);
        layout->addWidget(titleText);
        layout->addWidget(titleLargeText);
        layout->addWidget(displayText);

        m_styleTexts << captionText << bodyText << bodyStrongText << subtitleText 
                     << titleText << titleLargeText << displayText;

        return group;
    }

    // 创建图标文本展示组
    QGroupBox *createIconTextGroup()
    {
        QGroupBox *group = new QGroupBox("图标文本展示", this);
        QVBoxLayout *layout = new QVBoxLayout(group);

        QLabel *infoLabel = new QLabel("展示 ElaText 的图标字体支持", this);
        layout->addWidget(infoLabel);

        // 不同图标
        ElaText *infoIcon = new ElaText(this);
        infoIcon->setElaIcon(ElaIconType::CircleInfo);
        infoIcon->setTextPixelSize(24);

        ElaText *warningIcon = new ElaText(this);
        warningIcon->setElaIcon(ElaIconType::BrakeWarning);
        warningIcon->setTextPixelSize(24);

        ElaText *errorIcon = new ElaText(this);
        errorIcon->setElaIcon(ElaIconType::SkullCrossbones);
        errorIcon->setTextPixelSize(24);

        ElaText *successIcon = new ElaText(this);
        successIcon->setElaIcon(ElaIconType::ThumbsUp);
        successIcon->setTextPixelSize(24);

        // 图标和文本组合
        ElaText *comboText = new ElaText("带图标的文本", this);
        comboText->setElaIcon(ElaIconType::Star);
        comboText->setTextPixelSize(16);

        QHBoxLayout *iconLayout = new QHBoxLayout();
        iconLayout->addWidget(infoIcon);
        iconLayout->addWidget(warningIcon);
        iconLayout->addWidget(errorIcon);
        iconLayout->addWidget(successIcon);
        iconLayout->addStretch();

        layout->addLayout(iconLayout);
        layout->addWidget(comboText);

        m_iconTexts << infoIcon << warningIcon << errorIcon << successIcon << comboText;

        return group;
    }

    // 创建换行模式展示组
    QGroupBox *createWrapModeGroup()
    {
        QGroupBox *group = new QGroupBox("换行模式展示", this);
        QVBoxLayout *layout = new QVBoxLayout(group);

        QLabel *infoLabel = new QLabel("展示不同的文本换行模式", this);
        layout->addWidget(infoLabel);

        // 普通换行文本
        ElaText *normalWrapText = new ElaText(
            "这是普通换行模式的文本，会在单词边界处换行。这是一个很长的文本用于演示换行效果。", this);
        normalWrapText->setWordWrap(true);
        normalWrapText->setIsWrapAnywhere(false);
        normalWrapText->setFixedWidth(300);

        // 任意位置换行文本
        ElaText *anywhereWrapText = new ElaText(
            "这是任意位置换行模式的文本，可以在任意字符位置换行。这是一个很长的文本用于演示换行效果。", this);
        anywhereWrapText->setWordWrap(true);
        anywhereWrapText->setIsWrapAnywhere(true);
        anywhereWrapText->setFixedWidth(300);

        layout->addWidget(new QLabel("普通换行模式:", this));
        layout->addWidget(normalWrapText);
        layout->addWidget(new QLabel("任意位置换行模式:", this));
        layout->addWidget(anywhereWrapText);

        m_wrapTexts << normalWrapText << anywhereWrapText;

        return group;
    }

    // 创建动态操作演示组
    QGroupBox *createDynamicOperationGroup()
    {
        QGroupBox *group = new QGroupBox("动态操作演示", this);
        QVBoxLayout *layout = new QVBoxLayout(group);

        QLabel *infoLabel = new QLabel("演示 ElaText 的动态操作功能", this);
        layout->addWidget(infoLabel);

        // 动态修改文本
        QHBoxLayout *textChangeLayout = new QHBoxLayout();
        m_dynamicText = new ElaText("原始文本内容", this);
        QPushButton *changeTextBtn = new QPushButton("修改文本", this);

        textChangeLayout->addWidget(m_dynamicText);
        textChangeLayout->addWidget(changeTextBtn);
        textChangeLayout->addStretch();

        // 动态修改字体大小
        QHBoxLayout *sizeChangeLayout = new QHBoxLayout();
        m_sizeChangeText = new ElaText("字体大小演示文本", this);
        QPushButton *increaseSizeBtn = new QPushButton("增大字体", this);
        QPushButton *decreaseSizeBtn = new QPushButton("减小字体", this);

        sizeChangeLayout->addWidget(m_sizeChangeText);
        sizeChangeLayout->addWidget(increaseSizeBtn);
        sizeChangeLayout->addWidget(decreaseSizeBtn);
        sizeChangeLayout->addStretch();

        // 动态切换样式
        QHBoxLayout *styleChangeLayout = new QHBoxLayout();
        m_styleChangeText = new ElaText("样式切换演示文本", this);
        QPushButton *toggleStyleBtn = new QPushButton("切换样式", this);

        styleChangeLayout->addWidget(m_styleChangeText);
        styleChangeLayout->addWidget(toggleStyleBtn);
        styleChangeLayout->addStretch();

        // 动态切换图标
        QHBoxLayout *iconChangeLayout = new QHBoxLayout();
        m_iconChangeText = new ElaText(this);
        m_iconChangeText->setTextPixelSize(20);
        QPushButton *changeIconBtn = new QPushButton("切换图标", this);

        iconChangeLayout->addWidget(m_iconChangeText);
        iconChangeLayout->addWidget(changeIconBtn);
        iconChangeLayout->addStretch();

        layout->addLayout(textChangeLayout);
        layout->addLayout(sizeChangeLayout);
        layout->addLayout(styleChangeLayout);
        layout->addLayout(iconChangeLayout);

        m_dynamicTexts << m_dynamicText << m_sizeChangeText << m_styleChangeText << m_iconChangeText;
        m_operationButtons << changeTextBtn << increaseSizeBtn << decreaseSizeBtn << toggleStyleBtn << changeIconBtn;

        return group;
    }

    // 创建实际应用场景组
    QGroupBox *createApplicationGroup()
    {
        QGroupBox *group = new QGroupBox("实际应用场景", this);
        QVBoxLayout *layout = new QVBoxLayout(group);

        QLabel *infoLabel = new QLabel("展示 ElaText 在实际界面中的应用", this);
        layout->addWidget(infoLabel);

        // 卡片标题
        ElaText *cardTitle = new ElaText("用户信息卡片", this);
        cardTitle->setTextStyle(ElaTextType::Title);

        // 用户信息
        ElaText *userName = new ElaText("张三", this);
        userName->setTextStyle(ElaTextType::Subtitle);

        ElaText *userEmail = new ElaText("zhangsan@example.com", this);
        userEmail->setTextStyle(ElaTextType::Body);

        ElaText *userBio = new ElaText("这是一段用户简介，描述用户的基本信息和兴趣爱好。", this);
        userBio->setWordWrap(true);
        userBio->setIsWrapAnywhere(true);

        // 状态信息
        QHBoxLayout *statusLayout = new QHBoxLayout();
        ElaText *statusIcon = new ElaText(this);
        statusIcon->setElaIcon(ElaIconType::ThumbsUp);
        statusIcon->setTextPixelSize(16);

        ElaText *statusText = new ElaText("在线", this);
        statusText->setTextStyle(ElaTextType::Caption);

        statusLayout->addWidget(statusIcon);
        statusLayout->addWidget(statusText);
        statusLayout->addStretch();

        layout->addWidget(cardTitle);
        layout->addWidget(userName);
        layout->addWidget(userEmail);
        layout->addWidget(userBio);
        layout->addLayout(statusLayout);

        m_applicationTexts << cardTitle << userName << userEmail << userBio << statusIcon << statusText;

        return group;
    }

    void setupConnections()
    {
        // 动态文本修改
        connect(m_operationButtons[0], &QPushButton::clicked, this, [this]() {
            static int clickCount = 0;
            clickCount++;
            QString newText = QString("动态修改的文本内容 - 点击次数: %1").arg(clickCount);
            m_dynamicText->setText(newText);
            qDebug() << "文本已修改为:" << newText;
        });

        // 增大字体
        connect(m_operationButtons[1], &QPushButton::clicked, this, [this]() {
            int currentSize = m_sizeChangeText->getTextPixelSize();
            m_sizeChangeText->setTextPixelSize(currentSize + 2);
            qDebug() << "字体大小增大为:" << currentSize + 2;
        });

        // 减小字体
        connect(m_operationButtons[2], &QPushButton::clicked, this, [this]() {
            int currentSize = m_sizeChangeText->getTextPixelSize();
            if (currentSize > 8) {
                m_sizeChangeText->setTextPixelSize(currentSize - 2);
                qDebug() << "字体大小减小为:" << currentSize - 2;
            }
        });

        // 切换样式
        connect(m_operationButtons[3], &QPushButton::clicked, this, [this]() {
            static int styleIndex = 0;
            ElaTextType::TextStyle styles[] = {
                ElaTextType::Body,
                ElaTextType::BodyStrong,
                ElaTextType::Subtitle,
                ElaTextType::Title
            };
            
            styleIndex = (styleIndex + 1) % 4;
            m_styleChangeText->setTextStyle(styles[styleIndex]);
            qDebug() << "文本样式切换为:" << styleIndex;
        });

        // 切换图标
        connect(m_operationButtons[4], &QPushButton::clicked, this, [this]() {
            static int iconIndex = 0;
            ElaIconType::IconName icons[] = {
                ElaIconType::Info,
                ElaIconType::TriangleExclamation,
                ElaIconType::SkullCrossbones,
                ElaIconType::ThumbsUp,
                ElaIconType::Star
            };
            
            iconIndex = (iconIndex + 1) % 5;
            m_iconChangeText->setElaIcon(icons[iconIndex]);
            qDebug() << "图标切换为:" << iconIndex;
        });

        //// 主题切换响应
        //connect(eTheme, &ElaTheme::themeModeChanged, this, [this](ElaThemeType::ThemeMode mode) {
        //    qDebug() << "主题已切换，所有 ElaText 控件已自动更新颜色";
        //});
    }

private:
    // 各种演示用的文本控件
    QList<ElaText*> m_styleTexts;
    QList<ElaText*> m_iconTexts;
    QList<ElaText*> m_wrapTexts;
    QList<ElaText*> m_dynamicTexts;
    QList<ElaText*> m_applicationTexts;
    QList<QPushButton*> m_operationButtons;

    // 动态操作控件指针
    ElaText *m_dynamicText;
    ElaText *m_sizeChangeText;
    ElaText *m_styleChangeText;
    ElaText *m_iconChangeText;
};

int main(int argc, char *argv[])
{
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
    QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
    QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
#else
    qputenv("QT_SCALE_FACTOR", "1.5");
#endif
#endif

    QApplication app(argc, argv);
    eApp->init();

    ElaTextTest demo;
    demo.moveToCenter();
    demo.show();

    return app.exec();
}

#include "ElaTextTest.moc"